<template>
  <div v-loading="loading">
    <el-table
      :data="dseClassList"
      style="width: 100%"
      :height="tableHeight"
      empty-text="查询条件下没有找到记录"
    >
      <el-table-column prop="ClassName" label="班级名" width="180" />
      <el-table-column label="年级" width="180">
        <template #default="s">{{ enumVal2str(DseGradeEnum, s.row.DseGrade) }}</template>
      </el-table-column>
      <el-table-column prop="CreateTime" label="创建时间" width="180" />
      <el-table-column prop="Creator" label="创建人" width="180">
        <template #default="s">{{ s.row.Creator._v }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="s">
          <el-button link type="primary" @click="toDetail(s.row.Pk)"> 详情 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-loading="loading" class="my_opt_btn_pannel">
      <div style="text-align: left">
        <el-pagination
          hide-on-single-page
          background
          layout="prev, pager, next"
          :total="totalCount"
          :page-size="pageSize"
          v-model:current-page="pageIndex"
          @current-change="doSearch"
        />
      </div>
      <div style="text-align: right; padding-right: 20px">
        <el-button type="primary" v-if="userStore.isAdm" @click="toDetail()">创建</el-button>
        <el-button @click="searchVisable = true">搜索</el-button>
        <el-button @click="doSearch()">刷新</el-button>
      </div>
    </div>
    <el-dialog v-model="searchVisable" title="搜索条件" width="600">
      <el-form :model="search" class="demo-form-inline">
        <el-form-item label="班级名">
          <el-input v-model="search.s_ClassName" clearable />
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" @click="doSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { listDseClass } from '@/api/dseclass'
import type { DseClassModel } from '@/type/DseClass'
import { DseGradeEnum } from '@/type/DseClass'
import { computed, onMounted, reactive, ref } from 'vue'
import { useWindowSize } from '@vueuse/core'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { enumVal2str } from '@/utils/stringUtils'

const userStore = useUserStore()
const searchVisable = ref(false)
const pageSize = ref(50)
const pageIndex = ref(1)
const totalCount = ref(0)
const winSize = useWindowSize()
const loading = ref(false)
const dseClassList = ref<DseClassModel[]>()
const tableHeight = computed(() => winSize.height.value - 60 - 8 - 42) //60是MENU,8是PADDING-TOP，42是按钮区
const router = useRouter()
const search = reactive({
  s_ClassName: '',
})
onMounted(async () => {
  await doSearch()
})
const doSearch = async () => {
  searchVisable.value = false
  try {
    loading.value = true
    const cond = {
      S_ClassName: `lk(%${search.s_ClassName}%)`,
      pageIndex: pageIndex.value - 1,
      pageSize: pageSize.value,
    }
    const r = await listDseClass(cond)
    dseClassList.value = r.Data
    totalCount.value = r.TotalCount
  } finally {
    loading.value = false
  }
}
const toDetail = (pk?: string) => {
  router.push({ path: '/DseClassDetail', query: { pk: pk } })
}
</script>
